<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>详情</title>
</head>
<body>
<div>
    <h1>{{weibo.title}}</h1>
    <p>{{weibo.content}}</p>
    <p>作者:{{weibo.author}} ----------- 发布于{{weibo.created}}</p>
    <img :src="weibo.url" style="width: 300px" alt="">
    <hr>
    评论:
    <form action="">
        <input type="text" name="content" placeholder="发表评论.....">
        <input type="button" value="发布评论" @click="send()">
    </form>
    <hr>
    <h3>评论列表:</h3>
    <div v-for="comm in arr">
        <h4>{{comm.author}}:{{comm.content}}</h4>
        发布于:{{comm.created}}
        <!--如果评论的是用户id 等于当前登录的用户id 则显示删除-->
        <a v-if="comm.userId == user.id" href="javascript:void(0)" @click="del(comm.id)">删除</a>
    </div>
</div>

<!--引入axios和vue-->
<script src="js/axios.min.js"></script>
<script src="js/vue.js"></script>
<!--时间格式插件-->
<script src="js/moment.min.js"></script>

<script>
    let v = new Vue({
        el:"body>div",
        data:{
            arr:[],
            user:{},
            weibo:{title:"微博标题"}
        },
        created:function (){
            axios.get("/currentUser").then(function (response) {
                v.user = response.data;
            })
          //请求微博详细信息
          let id = location.search.split("=")[1];
          console.log("id = "+id);
          axios.get("/weibo/selectById?id="+id).then(function (response) {
              v.weibo = response.data;
              //将日期进行格式化操作
              v.weibo.created = moment(v.weibo.created).format("YYYY年MM月DD日 HH:mm:ss")
          })
          axios.get("/comment/select?id="+id).then(function (response) {
              v.arr = response.data;
              for (let c of v.arr) {
                  c.created = moment(c.created).format("YYYY年MM月DD日 HH:mm:ss")
              }
          })
        },
        methods:{
            send:function () {
                //得到form表单数据
                let data = new FormData(document.querySelector("form"));
                //把当前需要评论的微博id添加到请求参数中
                data.append("weiboId",v.weibo.id);
                axios.post("/comment/insert",data).then(function (response) {
                    if (response.data == 1){
                        alert("评论成功");
                        location.reload();//刷新页面
                    }else {
                        alert("请先登录!");
                        location.href = "/login.html";
                    }
                })
            },
            del:function (id) {
                if (confirm("确定删除评论")){
                    axios.get("/comment/delete?id="+id).then(function () {
                        location.reload();
                    })
                }
            }
        }
    })
</script>
</body>
</html>